//------------------------------------------------------------------------------
// <copyright file="WindowExceptionReport.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Windows;
using System.Windows.Controls;
using DAnton.Utilities.Extensions;
using DotBeer.Business.Components;
using DotBeer.Business.Entities.ErrorReporting;

namespace DotBeer.UI.WPFClient.Views
{
    /// <summary>
    /// Interaction logic for WindowExceptionReport.xaml
    /// </summary>
    public partial class WindowExceptionReport : Window
    {
        #region private members

        Exception exception;
        ErrorReportComponent errorLogic;
        CircleProgressBarControl circleProgressBar;

        #endregion

        #region constructors

        public WindowExceptionReport(Exception exception)
        {
            InitializeComponent();

            this.SetOwner();

            this.exception = exception;
            this.textBoxErrorInformation.Text = String.Format("{0}\n\n{1}", exception.Message, exception.StackTrace);
        }

        #endregion

        #region private methods

        private void buttonSend_Click(object sender, RoutedEventArgs e)
        {
            if (this.errorLogic == null)
            {
                this.errorLogic = new ErrorReportComponent();
                this.errorLogic.ReportErrorCompleted += new EventHandler<ReportErrorCompletedEventArgs>(errorLogic_ReportErrorCompleted);
            }

            this.errorLogic.ReportErrorAsync(this.exception, this.textBoxUserInfo.Text);

            this.buttonDontSend.IsEnabled = false;
            this.buttonSend.IsEnabled = false;

            if (this.circleProgressBar == null)
            {
                this.circleProgressBar = new CircleProgressBarControl();
                this.circleProgressBar.Width = 30;
                this.circleProgressBar.Height = 30;
                this.circleProgressBar.SetValue(Grid.RowProperty, 3);
                this.circleProgressBar.HorizontalAlignment = HorizontalAlignment.Center;
                this.circleProgressBar.VerticalAlignment = VerticalAlignment.Center;
            }

            this.LayoutRoot.Children.Add(circleProgressBar);
        }

        private void buttonDontSend_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void errorLogic_ReportErrorCompleted(object sender, ReportErrorCompletedEventArgs e)
        {
            this.LayoutRoot.Children.Remove(this.circleProgressBar);

            if (e.Error != null)
            {
                this.ShowErrorMessage();
            }
            else if (e.Cancelled)
            {
                CustomMessageBox.Show("Sending error report has been cancelled.");
                this.Close();
            }
            else if (e.Result)
            {
                CustomMessageBox.Show("Report sent succesful. Thanks for reporting!", "Operation success");
                this.Close();
            }
            else
            {
                this.ShowErrorMessage();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var result = CustomMessageBox.Show("It's suggested to restart DotBeer. Continue using DotBeer may occur with data corruption. Do you want to restart DotBeer? ", 
                                               "Information",
                                               MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
                this.DialogResult = true;
            else
                this.DialogResult = false;
        }

        private void ShowErrorMessage()
        {
            CustomMessageBox.Show("Error reporting failed. Please try again.", "Operation failed");
            this.buttonDontSend.IsEnabled = true;
            this.buttonSend.IsEnabled = true;
        }

        #endregion
    }
}
